//
//  MNDLendViewController.m
//  minidai
//
//  Created by Vitas on 2018/7/12.
//  Copyright © 2018年 迷你贷. All rights reserved.
//

#import "MNDLendViewController.h"
#import "MNDFinaceListPageViewController.h"
#import "MNDAutoTenderPlusViewController.h"
#import "UIImage+JKColor.h"
#import "MNDCreditListViewController.h"
#import "MNDLoginUserModel.h"

@interface MNDLendViewController ()

@property(nonatomic, strong) MNDFinaceListPageViewController *financeVC;
@property(nonatomic, strong) UIButton *itemButton;

@end

@implementation MNDLendViewController

- (NSArray<NSString *> *)titles {
    
    if ([MNDLoginUserModel isUserLogin]) {
        return @[@"自助投",@"散标",@"债权转让"];
    }else {
        return @[@"自助投",@"散标"];
    }
    
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.menuItemWidth = 70;
        self.menuViewStyle = WMMenuViewStyleLine;
        self.menuHeight = 64;
        self.itemMargin = 20;
        self.menuViewLayoutMode = WMMenuViewLayoutModeCenter;
        self.showOnNavigationBar = YES;
        self.titleSizeSelected = 16.0;
        self.titleSizeNormal = 16.0;
        self.titleColorNormal = [UIColor blackColor];
        self.titleColorSelected = [UIColor colorWithRed:255/255.0 green:99/255.0 blue:57/255.0 alpha:1.0];
        self.menuBGColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.navigationController.navigationBar setBackgroundImage:[UIImage jk_imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
//    [self initialBarbuttonItems];
//    [self initialLeftBarbuttonItems];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goTenderActionToIndex:) name:@"GoTenderActionNotification" object:nil];
    

}

- (void)goTenderActionToIndex:(NSNotification *)notification {
    int index = [notification.object intValue];
    self.selectIndex = index;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage: [UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
   
    [self reloadData];
}

- (void)initialBarbuttonItems {
    
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    
    UIImage *image = [UIImage imageNamed:@"finance_invest_btn-1"];
    self.itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.itemButton setImage:image forState:UIControlStateNormal];
    [self.itemButton addTarget:self action:@selector(navClickMore:) forControlEvents:UIControlEventTouchUpInside];
    self.itemButton.hidden = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:self.itemButton];
    
    [item setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size:14.0]} forState:UIControlStateNormal];
    [item setTintColor:kColorBackButtonColor];
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.rightBarButtonItem = item;
    } else {
        self.navigationItem.rightBarButtonItems = @[spaceItem, item];
    }
    
   
    
}

 //创建左边占位item
- (void)initialLeftBarbuttonItems {
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    UIImage *image = [UIImage imageNamed:@"finance_invest_btn-1"];
    UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    itemButton.hidden = YES;
    [itemButton setImage:image forState:UIControlStateNormal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:itemButton];
    [item setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size:14.0]} forState:UIControlStateNormal];
    [item setTintColor:kColorBackButtonColor];
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.leftBarButtonItem = item;
    } else {
        self.navigationItem.leftBarButtonItems = @[spaceItem, item];
    }
}

- (void)navClickMore:(UIButton *)sender {
    if (!self.financeVC.navRightBtnClick) {
        //显示遮罩后让tabview不能滚动
        self.financeVC.financeListTableView.scrollEnabled = NO;
        //取消遮罩的隐藏
        self.financeVC.maskView.hidden = NO;
        //        self.mndFinanceNavMoreView.hidden = NO;
        [UIView animateWithDuration:0.25 animations:^{
            self.financeVC.maskView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        }];
        UITableViewHeaderFooterView *headerView = [self.financeVC.financeListTableView headerViewForSection:1];
        headerView.hidden = YES;
        //    self.navigationController.navigationBarHidden = YES;
        //        self.tabBarController.tabBar.hidden = YES;
        self.financeVC.navRightBtnClick = YES;
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            [self.financeVC hidenExitMaskView];
        }];
    }
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titles.count;
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    if (index == 0) {
        MNDAutoTenderPlusViewController *autoTenderVC = [UIStoryboard viewControllerWithIdentifier:@"MNDAutoTenderPlusViewController"];
       
        return autoTenderVC;
    }else if(index == 1){
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Finance" bundle:nil];
        MNDFinaceListPageViewController *financeVC = [storyBoard instantiateViewControllerWithIdentifier:@"MNDFinaceListPageViewController"];
        self.financeVC = financeVC;
        return financeVC;
    }else {

        MNDCreditListViewController *creditVC = [UIStoryboard viewControllerWithIdentifier:@"MNDCreditListViewController"];
        
        return creditVC;
    }
}


- (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    
        NSLog(@"info = %@",info);
        NSNumber *index = info[@"index"];
        if (index.integerValue == 0) {
            self.itemButton.hidden = YES;
        }else {
            self.itemButton.hidden = NO;
        }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
